import api from "../utils/api.js";

class NetworkUtils {

  /**
   * url 请求地址
   * success 成功的回调
   * fail 失败的回调
   */
  static getRequest(that, url, success, complete) {
    that.setData({
      finish: false
    })

    console.log(url);
    wx.request({
      url: getApp().globalData.url + url,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        console.log(res.data);
        success(that, res);
      },
      fail: function(res) {
        console.log(res);
      },
      complete: function() {
        that.setData({
          finish: true
        })
        wx.stopPullDownRefresh();
        try {
          if (typeof complete === "function") {
            complete();
          }
        } catch (e) {}
      }
    });
  }

  /**
   * url 请求地址
   * start 分页的起始位置，不传则不分页
   * data 参数对象
   * success 成功的回调
   * fail 失败的回调
   */
  static getJson(that, url, start, data) {
    let app = getApp();
    let len = arguments.length;
    let obj = null;
    if (len == 2) {
      obj = {}
    } else {
      obj = arguments[len - 1];
    }
    if (typeof arguments[2] === "number") {
      obj.page = start;
      obj.lenght = 20;
    }
    obj.cfrom = "weixin";
    obj.device = app.globalData.device;

    return this.postRequest(that, url, "GET", arguments[len - 1]);
  }

  /**
   * url 请求地址
   * start 分页的起始位置，不传则不分页
   * data 参数对象
   * success 成功的回调
   * fail 失败的回调
   */
  static postJson(that, url, start, data) {
    let app = getApp();
    let len = arguments.length;
    let obj = null;
    if (len == 2) {
      obj = {}
    } else {
      obj = arguments[len - 1];
    }
    if (typeof arguments[2] === "number") {
      obj.page = start;
      obj.lenght = 20;
    }
    obj.cfrom = "weixin";
    obj.device = app.globalData.device;

    return this.postRequest(that, url, "POST", arguments[len - 1]);
  }

  /**
   * that 页面指针
   * url 请求地址
   * start 从多少条数据开始求请
   * length 获取数据数量
   * data 求请参数
   * success 成功的回调
   */
  static postRequest(that, url, method, data) {
    that.setData({
      finish: false
    })

    console.log(data);
    return new Promise((resolve, reject) => wx.request({
      url: getApp().globalData.url + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: method,
      data: data,
      success: function(res) {
        console.log(res);
        if (res.data.code == 200) {
          resolve(res);
        } else {
          reject(res)
          if (that.toast) {
            that.toast.showToast(res.data.msg);
          }
          if (res.data.msg == "登录失效，请重新登录") {
            wx.removeStorageSync('jinxin_user_info')
            setTimeout(function() {
              wx.redirectTo({
                url: '/pages/main/Myself'
              })
            }, 2000)
          }
        }
      },
      fail: function(res) {
        reject(res)
        console.log(res);
      },
      complete: function() {
        that.setData({
          finish: true
        })
        wx.stopPullDownRefresh();
      },
    }));
  }


  /**
   * that 页面指针
   * url 请求地址
   * start 从多少条数据开始求请
   * length 获取数据数量
   * data 求请参数
   * success 成功的回调
   */
  static _postRequest(url, data) {
    return new Promise((resolve, reject) => wx.request({
      url: getApp().globalData.url + url,
      method: "POST",
      data: data,
      success: function(res) {
        if (res.data.status == 1) {
          resolve(res);
        }
      }
    }));
  }

  /**
   * that 页面指针
   * url 请求地址
   * name 文件key
   * data 求请参数
   * success 成功的回调
   * 选择本地文件上传
   */
  // chooseUploadFile(that, url, name, data, success) {
  //   that.finish = false;
  //   let obj = {};
  //   let len = arguments.length;
  //   if (len == 5) {
  //     obj = arguments[len - 3];
  //   }
  //   var method = arguments[len - 1];
  //   wx.chooseImage({
  //     count: 1, // 默认9
  //     sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
  //     sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
  //     success: function(res) {
  //       let tempFilePaths = res.tempFilePaths;
  //       wx.uploadFile({
  //         url: that.url + url,
  //         filePath: tempFilePaths[0],
  //         name: name,
  //         formData: obj,
  //         success: function(res) {
  //           let imgUrl = JSON.parse(res.data).data;
  //           method(that, imgUrl);
  //         },
  //         fail: function(res) {
  //           console.log(res);
  //         },
  //         complete: function() {
  //           that.finish = true;
  //         },
  //       })
  //     }
  //   })
  // }

  /**
   * that 页面指针
   * url 请求地址
   * name 文件key
   * data 求请参数
   * success 成功的回调
   * 文件上传
   */
  // uploadFile(that, url, src, name, data, success) {
  //   that.finish = false;
  //   let obj = {};
  //   let len = arguments.length;
  //   if (len == 6) {
  //     obj = arguments[len - 4];
  //   }
  //   var method = arguments[len - 1];
  //   wx.uploadFile({
  //     url: that.url + url,
  //     filePath: src,
  //     name: name,
  //     formData: obj,
  //     success: function(res) {
  //       let imgUrl = JSON.parse(res.data).data;
  //       method(that, imgUrl);
  //     },
  //     fail: function(res) {
  //       console.log(res);
  //     },
  //     complete: function() {
  //       that.finish = true;
  //     },
  //   })
  // }

  static getUrl() {
    let url = getApp().globalData.debug ? api.debugUrl : api.url;
    return url;
  }
}


module.exports = NetworkUtils;